#!/usr/bin/env sh

echo "::format disk"
cfdisk /dev/sda

mkfs.ext2 /dev/sda1
mkswap /dev/sda2
mkfs.ext4 /dev/sda3

echo "::mount /root:"
mkdir /mnt/exherbo && mount /dev/sda3 /mnt/exherbo && cd /mnt/exherbo
swapon /dev/sda2

echo "::set date:"
ntpdate ntp.aliyun.com

echo "::install stage3:"
cd /mnt/exherbo
# curl -O https://dev.exherbo.org/stages/exherbo-x86_64-pc-linux-gnu-current.tar.xz
scap damon@192.168.3.240:/home/exherbo-x86_64-pc-linux-gnu-current.tar.xz .
curl -O https://dev.exherbo.org/stages/sha1sum
grep exherbo-x86_64-pc-linux-gnu-current.tar.xz sha1sum | sha1sum -c
tar xJpf exherbo-x86_64-pc-linux-gnu-current.tar.xz

echo "::update fstab:"
lsblk -f
sleep 3
nano -w /mnt/xherbo/etc/fstab

echo "::Mount everything for the chroot:"
mount -o rbind /dev /mnt/exherbo/dev/
mount -o rbind /sys /mnt/exherbo/sys/
mount -t proc none /mnt/exherbo/proc/
mount /dev/sda1 /mnt/exherbo/boot/

echo "::config portage:"
cp /etc/resolv.conf /mnt/exherbo/etc/resolv.conf

echo "::chroot to new root:"
env -i TERM=$TERM SHELL=/bin/bash HOME=$HOME $(which chroot) /mnt/exherbo /bin/bash
source /etc/profile
export PS1="(chroot) $PS1"

ping -c 5 baidu.com

echo "::Update the install"
cd /etc/paludis && vim bashrc && vim *conf
cave sync

echo "::Kernel install:"
cd path-to kernel
make nconfig
make
make modules_install
cp arch/x86/boot/bzImage /boot/kernel
tree /boot

echo "::Install an init system:"
cave resolve world -c
grub-install /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

echo "::For an Intel wireless card install the Linux firmware files"
cave resolve linux-firmware

echo "::Set root passwd:"
passwd
echo "::Add user:"
useradd -m -G users,wheel,audio -s /bin/bash damon
passwd damon

echo "::Set locale:"
localedef -i en_US -f ISO-8859-1 en_US
localedef -i en_US -f UTF-8 en_US.UTF-8
echo LANG="en_US.UTF-8" > /etc/env.d/99locale

echo "::Set system timezone"
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# exit
echo "::Finish: now you can 'exit && reboot'."
